package com.mfp.java.testYou.core;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import com.mfp.java.testYou.core.utils.UrlUtils;
import com.mfp.java.testYou.core.utils.Utils;
import com.mfp.java.testYou.db.DataAccessFactory;
import com.mfp.java.testYou.db.DataAccessObject;
import com.mfp.java.testYou.model.Language;

/**
 * This class will be initilized at startup.
 */
public class AppContext {

	private static final Logger log = Logger.getLogger(AppContext.class);

	public static final String CONFIG_FILE = "config.properties";
	private static final String CONFIG_FOLDER = "WEB-INF" + File.separator + "config";
	public static final String LOG4J = "log4j.xml";

	private static ServletContext servletContext;
	private static DataAccessObject dao;
	private static boolean forceAuth = true;
	private static List<Language> langs;

	/**
	 * Will initialize log4J
	 */
	private static void initLog4j() {
		String log4j = AppContext.getConfigPath() + File.separator + LOG4J;
		DOMConfigurator.configure(log4j);
		log.info("Loaded log config from " + log4j);
	}

	/**
	 * should be fired at the start of the application. After init, and after
	 * log init.
	 */
	private static void initDb() {
		try {
			dao = DataAccessFactory.createDAO();
		} catch (InstantiationException e) {
			log.error("Can't create DAO: " + e.getClass().getName() + " : " + e.getMessage());
		} catch (IllegalAccessException e) {
			log.error("Can't create DAO: " + e.getClass().getName() + " : " + e.getMessage());
		} catch (ClassNotFoundException e) {
			log.error("Can't create DAO: " + e.getClass().getName() + " : " + e.getMessage());
		}
	}

	/**
	 * should be fired at the start of the application
	 * 
	 * @param ctx
	 *            servlet context (application context)
	 */
	public static void init(ServletContext ctx) {
		servletContext = ctx;
		forceAuth = new Boolean(Utils.readConfig(CONFIG_FILE, "app.forceAuthentication"));
		initLog4j();
		initDb();
		langs = LanguagesFactory.createList();
	}

	/**
	 * Will return the path: CATALINA_HOME/MY_WEBAPP/WEB-INF/config <br/>
	 * Basically the location of the config files.
	 * 
	 * @return string (path of dir)
	 */
	public static String getConfigPath() {
		return servletContext.getRealPath(CONFIG_FOLDER);
	}

	/**
	 * Will return the path: CATALINA_HOME/MY_WEBAPP/<br/>
	 * Basically the location of the webapp.
	 * 
	 * @return string (path of dir)
	 */
	public static String getBasePath() {
		return servletContext.getRealPath("/");
	}

	/**
	 * 
	 * @return the data access object, Used to access the database
	 */
	public static DataAccessObject getDAO() {
		return dao;
	}

	/**
	 * @return true is authentication is required. <br/>
	 *         Set to false for development.
	 */
	public static boolean isForceAuth() {
		return forceAuth;
	}

	/**
	 * Will return the current users testYouSession
	 * 
	 * @param req
	 *            ServletRquest
	 * @return Initialized UserSession object
	 */
	public static UserSession getUserSession(ServletRequest req) {
		HttpServletRequest request = UrlUtils.wrapRequest(req);
		Object o = request.getSession().getAttribute(UserSession.ATTRIBUTE_NAME);
		if (o instanceof UserSession) {
			UserSession sess = (UserSession) o;
			return sess;
		}
		return new UserSession(null);
	}

	/**
	 * Will return a list of allowed languages.
	 * @return List of lang.
	 */
	public static List<Language> getLangs() {
		return langs;
	}

}
